// BY BC Chong


/// <reference name="MicrosoftAjaxTimer.debug.js" />
/// <reference name="MicrosoftAjaxWebForms.debug.js" />
/// <reference name="Sys.Extended.UI.ExtenderBase.BaseScripts.js" assembly="AjaxControlToolkit" />


Type.registerNamespace('AjaxControlToolkitEx');

AjaxControlToolkitEx.AutoSizeType = function() {
    throw Error.invalidOperation();
}
AjaxControlToolkitEx.AutoSizeType.prototype = {
    None : 0,
    Extend : 1,
    Grow : 2
}
AjaxControlToolkitEx.AutoSizeType.registerEnum("AjaxControlToolkitEx.AutoSizeType", false);

AjaxControlToolkitEx.ScrollableGridViewExtenderBehavior = function(element) {
    AjaxControlToolkitEx.ScrollableGridViewExtenderBehavior.initializeBase(this, [element]);

	this._containerPanelID = null;
    this._hiddenFieldID = null;
    
    this._horizontalAutoSize = AjaxControlToolkitEx.AutoSizeType.None;
    this._verticalAutoSize = AjaxControlToolkitEx.AutoSizeType.None;
    this._autoSizeWidthOffset = 0;
    this._autoSizeHeightOffset = 0;
    
    this._freezeHeader = true;
    this._freezeFooter = true;
    this._freezeColumnsIndex = '';
    this._freezeHeaderStyleCssClass = '';
    this._freezeColumnStyleCssClass = '';
    this._freezeColumnHeaderStyleCssClass = '';
    
    this._resizeDelegate = null;
}

AjaxControlToolkitEx.ScrollableGridViewExtenderBehavior.prototype = {
    initialize : function() {
        AjaxControlToolkitEx.ScrollableGridViewExtenderBehavior.callBaseMethod(this, 'initialize');

		var pnl = $get(this.get_ContainerPanelID());
        var gv = this.get_element();
        var hf = $get(this.get_HiddenFieldID());        

        pnl.style.position = 'relative';
        pnl.style.overflow = 'auto';
        
        
        if (this.get_HorizontalAutoSize() == AjaxControlToolkitEx.AutoSizeType.Extend
			|| this.get_VerticalAutoSize() == AjaxControlToolkitEx.AutoSizeType.Extend){
			if (this._resizeDelegate === null) {
				this._resizeDelegate = Function.createDelegate(this, this._resizeHandler);
			}
			$addHandler(window, 'resize', this._resizeDelegate);
			this._resizeHandler(null);
		}
		        
		this._resizeHandler(null);
        
		this.registerPartialUpdateEvents();
    },

    dispose : function() {
        		
        if (this._resizeDelegate) {
			$removeHandler(window, 'resize', this._resizeDelegate);
            delete this._resizeDelegate;
        }        
        		
        AjaxControlToolkitEx.ScrollableGridViewExtenderBehavior.callBaseMethod(this, 'dispose');
    },
    
    _partialUpdateEndRequest : function(sender, endRequestEventArgs) {
        /// <param name="sender" type="Object">
        /// Sender
        /// </param>
        /// <param name="endRequestEventArgs" type="Sys.WebForms.EndRequestEventArgs">
        /// Event arguments
        /// </param>
        AjaxControlToolkitEx.ScrollableGridViewExtenderBehavior.callBaseMethod(this, '_partialUpdateEndRequest', [sender, endRequestEventArgs]);

        if (this.get_element()) {
            // Look up result by element's ID
            var action = endRequestEventArgs.get_dataItems()[this.get_element().id];
        }
    },
    
    get_HiddenFieldID : function() {
        return this._hiddenFieldID;
    },
    set_HiddenFieldID : function(value) {
        if (this._hiddenFieldID != value) { 
            this._hiddenFieldID = value;
            this.raisePropertyChanged('HiddenFieldID');
        }
    },

    get_ContainerPanelID : function() {
        return this._containerPanelID;
    },
    set_ContainerPanelID : function(value) {
        if (this._containerPanelID != value) { 
            this._containerPanelID = value;
            this.raisePropertyChanged('ContainerPanelID');
        }
    },

    get_HorizontalAutoSize : function() {
        return this._horizontalAutoSize;
    },
    set_HorizontalAutoSize : function(value) {
        if (this._horizontalAutoSize != value) {
            this._horizontalAutoSize = value;
            this.raisePropertyChanged('HorizontalAutoSize');
        }
    },

    get_VerticalAutoSize : function() {
        return this._verticalAutoSize;
    },
    set_VerticalAutoSize : function(value) {
        if (this._verticalAutoSize != value) {
            this._verticalAutoSize = value;
            this.raisePropertyChanged('VerticalAutoSize');
        }
    },

    get_AutoSizeWidthOffset : function() {
        return this._autoSizeWidthOffset;
    },
    set_AutoSizeWidthOffset : function(value) {
        if (this._autoSizeWidthOffset != value) { 
            this._autoSizeWidthOffset = value;
            this.raisePropertyChanged('AutoSizeWidthOffset');
        }
    },

    get_AutoSizeHeightOffset : function() {
        return this._autoSizeHeightOffset;
    },
    set_AutoSizeHeightOffset : function(value) {
        if (this._autoSizeHeightOffset != value) { 
            this._autoSizeHeightOffset = value;
            this.raisePropertyChanged('AutoSizeHeightOffset');
        }
    },
    
    

    get_FreezeHeader : function() {
        return this._freezeHeader;
    },
    set_FreezeHeader : function(value) {
        if (this._freezeHeader != value) { 
            this._freezeHeader = value;
            this.raisePropertyChanged('FreezeHeader');
        }
    },
    
    get_FreezeFooter : function() {
        return this._freezeFooter;
    },
    set_FreezeFooter : function(value) {
        if (this._freezeFooter != value) { 
            this._freezeFooter = value;
            this.raisePropertyChanged('FreezeFooter');
        }
    },

    get_FreezeColumnsIndex : function() {
        return this._freezeColumnsIndex;
    },
    set_FreezeColumnsIndex : function(value) {
        if (this._freezeColumnsIndex != value) { 
            this._freezeColumnsIndex = value;
            this.raisePropertyChanged('FreezeColumnsIndex');
        }
    },
    
    get_FreezeHeaderStyleCssClass : function() {
        return this._freezeHeaderStyleCssClass;
    },
    set_FreezeHeaderStyleCssClass : function(value) {
        if (this._freezeHeaderStyleCssClass != value) { 
            this._freezeHeaderStyleCssClass = value;
            this.raisePropertyChanged('FreezeHeaderStyleCssClass');
        }
    },

    get_FreezeColumnStyleCssClass : function() {
        return this._freezeColumnStyleCssClass;
    },
    set_FreezeColumnStyleCssClass : function(value) {
        if (this._freezeColumnStyleCssClass != value) { 
            this._freezeColumnStyleCssClass = value;
            this.raisePropertyChanged('FreezeColumnStyleCssClass');
        }
    },

    get_FreezeColumnHeaderStyleCssClass : function() {
        return this._freezeColumnHeaderStyleCssClass;
    },
    set_FreezeColumnHeaderStyleCssClass : function(value) {
        if (this._freezeColumnHeaderStyleCssClass != value) { 
            this._freezeColumnHeaderStyleCssClass = value;
            this.raisePropertyChanged('FreezeColumnHeaderStyleCssClass');
        }
    },
    
    _resizeHandler : function(e) {
    
		if (this.get_HorizontalAutoSize() == AjaxControlToolkitEx.AutoSizeType.None
			&& this.get_VerticalAutoSize() == AjaxControlToolkitEx.AutoSizeType.None) 
			return;
		var pnl = $get(this.get_ContainerPanelID());
		
        var clientBounds = $common.getClientBounds();
        var clientWidth = clientBounds.width;
        var clientHeight = clientBounds.height;
		
        var sz = $common.getSize(pnl);
        var pnlparentsz = $common.getSize(pnl.parentNode);
        if (this.get_HorizontalAutoSize() == AjaxControlToolkitEx.AutoSizeType.Extend)
        {
			sz.width = Math.max(100, clientWidth - this.get_AutoSizeWidthOffset());
		}else if (this.get_HorizontalAutoSize() == AjaxControlToolkitEx.AutoSizeType.Grow){
			var gv = this.get_element();
			var szGV = $common.getSize(gv);
			sz.width = szGV.width + 30;
		}else{
			sz.width = pnlparentsz.width;
		}
        if (this.get_VerticalAutoSize() == AjaxControlToolkitEx.AutoSizeType.Extend){
			sz.height = Math.max(100, clientHeight - this.get_AutoSizeHeightOffset());
		}else if (this.get_VerticalAutoSize() == AjaxControlToolkitEx.AutoSizeType.Grow){
			var gv = this.get_element();
			var szGV = $common.getSize(gv);
			sz.height = szGV.height + 30;
		}else{
			//sz.height = pnlparentsz.height;
			var gv = this.get_element();
			var szGV = $common.getSize(gv);
			sz.height = szGV.height + 30;			
		}
        $common.setSize(pnl, sz);
    }
    
}
AjaxControlToolkitEx.ScrollableGridViewExtenderBehavior.registerClass('AjaxControlToolkitEx.ScrollableGridViewExtenderBehavior', Sys.Extended.UI.BehaviorBase);
